<?php  

namespace Mobile\Controller;

use Api\Controller\BaseController;

use think\Controller;

use Common\Logic\LotteryLogic;

/*
 * 抽奖控制器
 *
 */
class LotteryController extends MobileBaseController
{
	
	public function  __construct() 
	{    
		// var_dump($_SERVER);
        parent::__construct();              
        if(session('?user'))
        {
        	$user = session('user');
            // $user = M('users')->where(array('user_id' => $user['user_id']))->find();
            // session('user',$user);  //覆盖session 中的 user               			                
        	$this->user = $user;
        	$this->user_id = $user['user_id'];
        }else{
        	// $this->error("请先登录");
        	$this->user_id = 2773;
        }

    }

	public function index()
	{ 
		if(!LotteryLogic::checkLotteryTime())
		{
			$this->error("暂无抽奖活动");
		}

		#奖品列表 
		$this->assign("prize_list",LotteryLogic::getPrizeList());
		#抽奖次数
		$this->assign("lottery_times", LotteryLogic::getUserLuckCount($this->user_id));
		#近期获奖用户  
		$this->assign("lottery_users",LotteryLogic::getLucker());
		$this->display();
	}

	/* 用户抽奖记录 */
    public function lottery_record()
    { 
    	if( IS_POST )
    	{
    		try{
	    		$res = LotteryLogic::getUserRecord($this->user_id);

	    		echo json_encode([
	    			"status" =>	"success",
	    			"code" => 1,
	    			"msg" => "获取成功",
	    			"data" => $res
	    		]);
	    		// var_dump($res);

	    	}catch(\Exception $e)
	    	{
	    		// var_dump($e);
	    		echo json_encode([
	    			"status" =>	"failed",
	    			"code" => $e->getCode(),
	    			"msg" => $e->getMessage(),
	    			"data" => ""
	    		]);
	    	}
    	}else{
    		$this->display();
    	}
    }


	/* 用户抽奖 */
    public function lotteryDraw()
    { 
    	try{
    		$res = LotteryLogic::lotteryDraw($this->user_id);

    		echo json_encode([
    			"status" =>	"success",
    			"code" => 1,
    			"msg" => "抽奖成功",
    			"data" => $res
    		]);
    		// var_dump($res);

    	}catch(\Exception $e)
    	{
    		// var_dump($e);
    		echo json_encode([
    			"status" =>	"failed",
    			"code" => $e->getCode(),
    			"msg" => $e->getMessage(),
    			"data" => ""
    		]);
    	}
    }
}